from flask import Blueprint, render_template, send_from_directory
import os

main_bp = Blueprint('main', __name__)

@main_bp.route('/')
def index():
    """主页路由 - 返回前端构建后的入口文件"""
    return send_from_directory('static', 'index.html')

@main_bp.route('/static/<path:filename>')
def static_files(filename):
    """静态文件路由"""
    return send_from_directory('static', filename)

# 可选：支持前端路由 history 模式，将未知路径回退到 index.html
@main_bp.route('/<path:path>')
def catch_all(path):
    if os.path.exists(os.path.join('app', 'static', path)):
        return send_from_directory('static', path)
    return send_from_directory('static', 'index.html')

# 提供 uploads 目录静态访问（用于本地上传回退后的文件访问）
@main_bp.route('/uploads/<path:filename>')
def uploaded_files(filename):
    return send_from_directory('uploads', filename)